package gps.recorder;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GPSRecorder2Activity extends Activity {
	private LocationManager lm;
	private LocationListener ll;
	private boolean runningFlg;
	private Logger logger;
	private RecorderThread rt;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		ll = new Mylocationlistener();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

		logger = new Logger();

		Button btn1 = (Button) findViewById(R.id.btn1);
		btn1.setOnClickListener(new View.OnClickListener() {
			private Logger stop_logger = new Logger("GPSLog_stops.txt");
			@Override
			public void onClick(View v) {
				Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				if (location == null) {
					Toast.makeText(GPSRecorder2Activity.this, "empty", Toast.LENGTH_SHORT).show();
				} else {
					String info = location.getLatitude() + " " + location.getLongitude() + " STOP";
					Toast.makeText(GPSRecorder2Activity.this, info, Toast.LENGTH_SHORT).show();
					stop_logger.writeToLogfile(info);
				}
			}
		});
		Button btn2 = (Button) findViewById(R.id.btn2);
		btn2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				logger.showLogContent(GPSRecorder2Activity.this);
			}
		});
		Button btn3 = (Button) findViewById(R.id.btn3);
		btn3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				logger.startNewLogfile(GPSRecorder2Activity.this);
			}
		});

		final Button btn4 = (Button) findViewById(R.id.btn4);
		btn4.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (runningFlg) {
					runningFlg = false;
					btn4.setText(R.string.button4_start);
					rt.stopflg = false;
				} else {
					// startService(new Intent(GPSRecorder2Activity.this,
					// RecordingService.class));
					Message msg = new Message();
					msg.arg1 = 1;
					// messageHandler.sendMessage(msg);
					runningFlg = true;
					btn4.setText(R.string.button4_stop);
					// logThread.start();
					rt = new RecorderThread();
					rt.start();
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		rt.stopflg = false;
		lm.removeUpdates(ll);
	}

	private class Mylocationlistener implements LocationListener {
		@Override
		public void onLocationChanged(Location location) {
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}

	private class RecorderThread extends Thread {
		public boolean stopflg = true;

		@Override
		public void run() {
			while (stopflg) {
				Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				String info = "";
				if (location != null) {
					info = location.getLatitude() + " " + location.getLongitude();
				}
				logger.writeToLogfile(info);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}